package com.ymatou.iapi.godmch.testcase;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.updateChannelsFlagBean;
import com.ymatou.iapi.godmch.service.createmchCall;
import com.ymatou.iapi.godmch.service.updateChannelsFlagCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
/**
*更新商户渠道商身份
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_updateChannelsFlag{
	private static updateChannelsFlagBean bean;
	private static updateChannelsFlagCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商户渠道商身份");
	}

	@Before
	public void caseUp() {
		bean=new updateChannelsFlagBean();
		call=new updateChannelsFlagCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * 
	 * @param isChannl=1 是渠道   isChannl=0不是渠道
	 * @throws InterruptedException
	 */
	private void check_verify(Integer isChannl) throws InterruptedException
	{
		mch_infoWapper mchInfoWapper = new mch_infoWapper();
		Map<String, Object> map = new HashMap();
		map.put("mchId", bean.getMchId());
		mch_info mchInfo = mchInfoWapper.selectby(map).get(0);
        Logger.verifyEquals("200", call.getStatusCode1(), "验证返回成功status值");
        Logger.verifyEquals(isChannl, mchInfo.getChannels(), "验证返回成功Channels");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateChannelsFlag_001() {
		Logger.start(true,"更新商户渠道商身份-商户不是渠道-更新为渠道");
		try {
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int mchId_agentId = DGUserCallService.UserCreateCall(usercreateBean);
			
			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			createmchCall createmchCall=GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			createmchBean=GodmchManageCallService.InitcreatemchBeanForAgent(createmchBean.getMchId(), mchId_agentId).get(0);
			createmchBean.setCountry("CHN");
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			bean.setMchId(mchId_agentId);
			bean.setChannels(true);
			call.setData(bean);
			call.callService();			
			check_verify(1);
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateChannelsFlag_002() {
		Logger.start(true,"更新商户渠道商身份-商户是渠道-更新为不是渠道");
		try {
			//比喻agent=0  channels =1,更新之后agent=0  channels =0, 这样的数据是脏数据，研发这边后期会修改
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			bean.setMchId(mchId);
			bean.setChannels(false);
			call.setData(bean);
			call.callService();			
			
			check_verify(0);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateChannelsFlag_003() {
		Logger.start(true,"更新商户渠道商身份-商户是渠道-重新更新为渠道");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			bean.setMchId(mchId);
			bean.setChannels(true);
			call.setData(bean);
			call.callService();			
			
			check_verify(1);		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateChannelsFlag_004() {
		Logger.start(true,"更新商户渠道商身份-商户不是渠道-重新更新为不是渠道");
		try {
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int mchId_agentId = DGUserCallService.UserCreateCall(usercreateBean);
			
			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			createmchCall createmchCall=GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			createmchBean=GodmchManageCallService.InitcreatemchBeanForAgent(createmchBean.getMchId(), mchId_agentId).get(0);
			createmchBean.setCountry("CHN");
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			bean.setMchId(mchId_agentId);
			bean.setChannels(false);
			call.setData(bean);
			call.callService();			
			check_verify(0);		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateChannelsFlag_005() {
		Logger.start(true,"更新商户渠道商身份-验证mchId为空-返回失败");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			//bean.setMchId(mchId);
			bean.setChannels(true);
			call.setData(bean);
			call.callService();			
			
	        Logger.verifyEquals("4001", call.getStatusCode1(), "验证返回成功status值");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}